﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Linker.TaskCenter.Services.Dto;
using Linker.TaskCenter.Web.Models;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Linker.TaskCenter.Services
{
    public class ServiceAppService : ApplicationService, IServiceAppService
    {
        private readonly IRepository<ServiceEntity, Guid> _repository;

        public ServiceAppService(IRepository<ServiceEntity, Guid> repository)
        {
            _repository = repository;
        }

        /// <summary>
        ///     服务注册
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<AjaxResponse> Register(CreateOrUpdateServiceDto input)
        {
            if (input.Id.HasValue)
            {
                var entity = await _repository.GetAsync(input.Id.Value);
                ObjectMapper.Map(input, entity);
                await _repository.UpdateAsync(entity);
            }
            else
            {
                var entity = new ServiceEntity(GuidGenerator.Create());
                ObjectMapper.Map(input, entity);
                await _repository.InsertAsync(entity);
            }

            return new AjaxResponse();
        }


        //  public async  Task<AjaxResponse> Update()

        /// <summary>
        ///     获取所有服务
        /// </summary>
        /// <returns></returns>
        public async Task<AjaxResponse<List<ServiceDto>>> GetList()
        {
            var list = await _repository.GetListAsync();
            return new AjaxResponse<List<ServiceDto>>(ObjectMapper.Map<List<ServiceEntity>, List<ServiceDto>>(list));
        }

        /// <summary>
        ///     获取服务详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AjaxResponse<ServiceDto>> Get(Guid id)
        {
            var entity = await _repository.GetAsync(id);
            return new AjaxResponse<ServiceDto>(ObjectMapper.Map<ServiceEntity, ServiceDto>(entity));
        }
    }
}